<template>
  <div 
    :class="[
      'bx-tabs',
      `bx-tabs--${type}`,
      `bx-tabs--${size}`,
      {
        'bx-tabs--closable': closable
      }
    ]"
  >
    <!-- 标签栏 -->
    <div class="bx-tabs__nav-wrap">
      <div class="bx-tabs__nav bx-glass">
        <div 
          v-for="tab in tabs"
          :key="tab.key"
          :class="[
            'bx-tabs__tab',
            {
              'bx-tabs__tab--active': tab.key === activeKey,
              'bx-tabs__tab--disabled': tab.disabled
            }
          ]"
          @click="handleTabClick(tab)"
        >
          <BxIcon v-if="tab.icon" :name="tab.icon" class="bx-tabs__tab-icon" />
          <span class="bx-tabs__tab-text">{{ tab.label }}</span>
          <BxIcon 
            v-if="closable && !tab.disabled"
            name="x"
            class="bx-tabs__tab-close"
            @click.stop="handleTabClose(tab)"
          />
        </div>
        
        <!-- 活动指示器 -->
        <div 
          v-if="type === 'line'"
          class="bx-tabs__ink-bar"
          :style="inkBarStyle"
        ></div>
      </div>
      
      <!-- 操作按钮 -->
      <div v-if="$slots.extra" class="bx-tabs__extra">
        <slot name="extra"></slot>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="bx-tabs__content">
      <div 
        v-for="tab in tabs"
        :key="tab.key"
        v-show="tab.key === activeKey"
        class="bx-tabs__panel"
      >
        <slot :name="tab.key" :tab="tab">
          {{ tab.content }}
        </slot>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, nextTick, onMounted, onUpdated } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxTabs',
  components: {
    BxIcon
  },
  props: {
    activeKey: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'line',
      validator: (value) => ['line', 'card'].includes(value)
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    closable: {
      type: Boolean,
      default: false
    },
    tabs: {
      type: Array,
      default: () => [],
      validator: (tabs) => {
        return tabs.every(tab => 
          typeof tab === 'object' && 
          typeof tab.key === 'string' &&
          typeof tab.label === 'string'
        )
      }
    }
  },
  emits: ['change', 'close'],
  setup(props, { emit }) {
    const inkBarStyle = ref({})
    const navRef = ref(null)

    const updateInkBar = async () => {
      if (props.type !== 'line') return
      
      await nextTick()
      
      const activeTab = document.querySelector('.bx-tabs__tab--active')
      if (activeTab) {
        const tabRect = activeTab.getBoundingClientRect()
        const navRect = activeTab.parentElement.getBoundingClientRect()
        
        inkBarStyle.value = {
          width: `${tabRect.width}px`,
          transform: `translateX(${tabRect.left - navRect.left}px)`
        }
      }
    }

    const handleTabClick = (tab) => {
      if (tab.disabled) return
      emit('change', tab.key)
      nextTick(() => {
        updateInkBar()
      })
    }

    const handleTabClose = (tab) => {
      emit('close', tab.key)
    }

    onMounted(() => {
      updateInkBar()
    })

    onUpdated(() => {
      updateInkBar()
    })

    return {
      inkBarStyle,
      navRef,
      handleTabClick,
      handleTabClose
    }
  }
}
</script>

<style scoped>
.bx-tabs {
  font-family: var(--bx-font-family);
}

.bx-tabs__nav-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--bx-spacing-md);
}

.bx-tabs__nav {
  position: relative;
  display: flex;
  align-items: center;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-medium);
  overflow: hidden;
}

.bx-tabs--card .bx-tabs__nav {
  background: transparent;
  border: none;
  border-radius: 0;
}

.bx-tabs__tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--bx-spacing-xs);
  padding: 12px 16px;
  cursor: pointer;
  transition: all var(--bx-transition-medium);
  color: var(--bx-text-secondary);
  border-radius: var(--bx-radius-small);
  margin: 2px;
}

.bx-tabs--small .bx-tabs__tab {
  padding: 8px 12px;
  font-size: 13px;
}

.bx-tabs--large .bx-tabs__tab {
  padding: 16px 20px;
  font-size: 16px;
}

.bx-tabs--card .bx-tabs__tab {
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-medium) var(--bx-radius-medium) 0 0;
  margin: 0 2px 0 0;
  border-bottom: none;
}

.bx-tabs__tab:hover {
  color: var(--bx-text-primary);
  background: var(--bx-glass-bg-hover);
}

.bx-tabs__tab--active {
  color: var(--bx-text-primary);
  background: rgba(0, 122, 255, 0.1);
}

.bx-tabs--card .bx-tabs__tab--active {
  background: var(--bx-glass-bg-hover);
  border-color: var(--bx-glass-border-hover);
  z-index: 1;
}

.bx-tabs__tab--disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.bx-tabs__tab--disabled:hover {
  color: var(--bx-text-secondary);
  background: transparent;
}

.bx-tabs__tab-icon {
  font-size: 14px;
}

.bx-tabs__tab-text {
  white-space: nowrap;
}

.bx-tabs__tab-close {
  font-size: 12px;
  opacity: 0.6;
  transition: opacity var(--bx-transition-medium);
}

.bx-tabs__tab-close:hover {
  opacity: 1;
}

.bx-tabs__ink-bar {
  position: absolute;
  bottom: 2px;
  left: 0;
  height: 2px;
  background: rgba(0, 122, 255, 0.8);
  border-radius: 1px;
  transition: all var(--bx-transition-medium);
}

.bx-tabs__extra {
  margin-left: var(--bx-spacing-md);
}

.bx-tabs__content {
  position: relative;
}

.bx-tabs__panel {
  animation: bx-tabs-fade-in 0.3s ease-out;
}

@keyframes bx-tabs-fade-in {
  0% {
    opacity: 0;
    transform: translateY(8px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-tabs__nav-wrap {
    flex-direction: column;
    align-items: stretch;
    gap: var(--bx-spacing-sm);
  }
  
  .bx-tabs__nav {
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  
  .bx-tabs__nav::-webkit-scrollbar {
    display: none;
  }
  
  .bx-tabs__tab {
    flex-shrink: 0;
    padding: 10px 12px;
    font-size: 13px;
  }
  
  .bx-tabs--small .bx-tabs__tab {
    padding: 6px 10px;
    font-size: 12px;
  }
  
  .bx-tabs--large .bx-tabs__tab {
    padding: 12px 16px;
    font-size: 15px;
  }
  
  .bx-tabs__extra {
    margin-left: 0;
  }
}
</style>

